/*
 * Copyright (c) 2017 zhangcm. All Rights Reserved.
 */
package com.jnzc.ocr.serv.shop.service.impl;

import com.google.common.collect.Lists;
import com.jnzc.ocr.common.utils.ArrayHandleUtils;
import com.jnzc.ocr.common.utils.CalculateUtils;
import com.jnzc.ocr.common.utils.DateUtils;
import com.jnzc.ocr.common.utils.ValidateUtils;
import com.jnzc.ocr.serv.shop.entity.ShopCountIndex;
import com.jnzc.ocr.serv.shop.entity.ShopCountRevenue;
import com.jnzc.ocr.serv.shop.filter.ShopCountIndexFilter;
import com.jnzc.ocr.serv.shop.filter.ShopCountRevenueFilter;
import com.jnzc.ocr.serv.shop.mapper.ShopCountMapper;
import com.jnzc.ocr.serv.shop.service.ShopCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author zhangcm
 * @version 1.0, 2017/10/7
 */
@Service
public class ShopCountServiceImpl implements ShopCountService {

    @Autowired
    private ShopCountMapper shopCountMapper;

    @Override
    public ShopCountRevenue findRevenue(ShopCountRevenueFilter filter) {
        ValidateUtils.notNull(filter.getBeginDate(), "开始时间");
        ValidateUtils.notNull(filter.getEndDate(), "结束时间");
        return shopCountMapper.findRevenue(filter);
    }

    @Override
    public List<ShopCountRevenue> listRevenue(ShopCountRevenueFilter filter) {
        ValidateUtils.notNull(filter.getBeginDate(), "开始时间");
        ValidateUtils.notNull(filter.getEndDate(), "结束时间");
        List<ShopCountRevenue> list = shopCountMapper.listRevenue(filter);
        Map<String, ShopCountRevenue> map = ArrayHandleUtils.parseMap(list, "day");
        List<ShopCountRevenue> result = Lists.newArrayList();
        List<String> dayList = DateUtils.listStr(filter.getBeginDate(), filter.getEndDate(), DateUtils.DEFAULT_FORMAT_DATE);
        for (String day : dayList) {
            ShopCountRevenue obj = map.get(day);
            if(null == obj){
                obj = new ShopCountRevenue();
                obj.setDay(day);
                obj.setNum(0);
                obj.setAmount(BigDecimal.ZERO);
            }
            result.add(obj);
        }
        return result;
    }

    @Override
    public ShopCountIndex countIndex(Long shopId) {
        ShopCountIndex result = new ShopCountIndex();
        // 今天
        ShopCountIndexFilter nowFilter = new ShopCountIndexFilter(shopId, DateUtils.getDay(), DateUtils.getDay(1));
        result.setNowAmount(shopCountMapper.countIncome(nowFilter));
        // 昨天
        ShopCountIndexFilter yestFilter = new ShopCountIndexFilter(shopId, DateUtils.getDay(-1), DateUtils.getDay());
        result.setYestAmount(shopCountMapper.countIncome(yestFilter));
        // 七天
        ShopCountIndexFilter sevenFilter = new ShopCountIndexFilter(shopId, DateUtils.getDay(), DateUtils.getDay(7));
        result.setSevenAmount(shopCountMapper.countIncome(sevenFilter));
        // 去年七天
        ShopCountIndexFilter perSevenFilter = new ShopCountIndexFilter(shopId, DateUtils.getDayWithAddYear(sevenFilter.getBeginDate(), -1), DateUtils.getDayWithAddYear(sevenFilter.getEndDate(), -1));
        result.setPerSevenAmount(shopCountMapper.countIncome(perSevenFilter));
        // 本月
        ShopCountIndexFilter monthFilter = new ShopCountIndexFilter(shopId, DateUtils.getFirstDayOfMonth(), DateUtils.getDayWithAddMonth(DateUtils.getFirstDayOfMonth(), 1));
        result.setMonthAmount(shopCountMapper.countIncome(monthFilter));
        // 去年本月
        ShopCountIndexFilter perMonthFilter = new ShopCountIndexFilter(shopId, DateUtils.getDayWithAddYear(monthFilter.getBeginDate(), -1), DateUtils.getDayWithAddYear(monthFilter.getEndDate(), -1));
        result.setPerMonthAmount(shopCountMapper.countIncome(perMonthFilter));

        result.setNowChangeAmount(CalculateUtils.sub(result.getNowAmount(), result.getYestAmount()));
        result.setSevenChangeAmount(CalculateUtils.sub(result.getSevenAmount(), result.getPerSevenAmount()));
        result.setMonthChangeAmount(CalculateUtils.sub(result.getMonthAmount(), result.getPerMonthAmount()));
        return result;
    }
}
